home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_13_11
/
phillip2
/
filter.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-23
|
15KB
|
598 lines
/***********************************************
*
* file d:\cips\filter.c
*
* Functions: This file contains
* filter_image
* median_filter
* high_pixel
* low_pixel
* setup_filters
* get_filter_options
* median_of
* sort_elements
* swap
*
* Purpose:
* These functions implement several
* types of basic spatial frequency
* filters.
*
* External Calls:
* wtiff.c - round_off_image_size
* create_file_if_needed
* write_array_into_tiff_image
* tiff.c - read_tiff_header
* rtiff.c - read_tiff_image
* numcvrt.c - get_integer
*
*
* Modifications:
* 15 February 1992 - created
*
*************************************************/
#include "cips.h"
/*******************************************
*
* Define the filter masks.
*
*******************************************/
short lpf_filter_6[3][3] =
{ {0, 1, 0},
{1, 2, 1},
{0, 1, 0}};
short lpf_filter_9[3][3] =
{ {1, 1, 1},
{1, 1, 1},
{1, 1, 1}};
short lpf_filter_10[3][3] =
{ {1, 1, 1},
{1, 2, 1},
{1, 1, 1}};
short lpf_filter_16[3][3] =
{ {1, 2, 1},
{2, 4, 2},
{1, 2, 1}};
short lpf_filter_32[3][3] =
{ {1, 4, 1},
{4, 12, 4},
{1, 4, 1}};
short hpf_filter_1[3][3] =
{ { 0, -1, 0},
{-1, 5, -1},
{ 0, -1, 0}};
short hpf_filter_2[3][3] =
{ {-1, -1, -1},
{-1, 9, -1},
{-1, -1, -1}};
short hpf_filter_3[3][3] =
{ { 1, -2, 1},
{-2, 5, -2},
{ 1, -2, 1}};
/*******************************************
*
* filter_image(...
*
* This function filters an image by using
* a single 3x3 mask.
*
*******************************************/
filter_image(in_name, out_name, the_image, out_image,
il, ie, ll, le, filter, type)
char in_name[], out_name[];
int il, ie, ll, le, type;
short filter[3][3],
the_image[ROWS][COLS],
out_image[ROWS][COLS];
{
int a, b, d, i, j, k,
length, max, sum, width;
struct tiff_header_struct image_header;
create_file_if_needed(in_name, out_name, out_image);
read_tiff_header(in_name, &image_header);
d = type;
if(type == 2 || type == 3) d = 1;
max = 255;
if(image_header.bits_per_pixel == 4)
max = 16;
read_tiff_image(in_name, the_image, il, ie, ll, le);
/* Do convolution over image array */
printf("\n");
for(i=1; i<ROWS-1; i++){
if( (i%10) == 0) printf("%d ", i);
for(j=1; j<COLS-1; j++){
sum = 0;
for(a=-1; a<2; a++){
for(b=-1; b<2; b++){
sum = sum +
the_image[i+a][j+b] *
filter[a+1][b+1];
}
}
sum = sum/d;
if(sum < 0) sum = 0;
if(sum > max) sum = max;
out_image[i][j] = sum;
} /* ends loop over j */
} /* ends loop over i */
fix_edges(out_image, 1);
write_array_into_tiff_image(out_name, out_image,
il, ie, ll, le);
} /* ends filter_image */
/*******************************************
*
* high_pixel(..
*
* This function replaces the pixel at
* the center of a 3x3, 5x5, etc. area
* with the max for that area.
*
*******************************************/
high_pixel(in_name, out_name, the_image, out_image,
il, ie, ll, le, size)
char in_name[], out_name[];
int il, ie, ll, le, size;
short the_image[ROWS][COLS],
out_image[ROWS][COLS];
{
int a, b, count, i, j, k,
length, sd2, sd2p1, ss, width;
short *elements;
struct tiff_header_struct image_header;
sd2 = size/2;
sd2p1 = sd2 + 1;
/**********************************************
*
* Allocate the elements array large enough
* to hold size*size shorts.
*
**********************************************/
ss = size*size;
elements = (short *) malloc(ss * sizeof(short));
create_file_if_needed(in_name, out_name, out_image);
read_tiff_image(in_name, the_image, il, ie, ll, le);
/***************************
*
* Loop over image array
*
****************************/
printf("\n");
for(i=sd2; i<ROWS-sd2; i++){
if( (i%10) == 0) printf("%d ", i);
for(j=sd2; j<COLS-sd2; j++){
count = 0;
for(a=-sd2; a<sd2p1; a++){
for(b=-sd2; b<sd2p1; b++){
elements[count] = the_image[i+a][j+b];
count++;
}
}
sort_elements(elements, &ss);
out_image[i][j] = elements[ss-1];
} /* ends loop over j */
} /* ends loop over i */
fix_edges(out_image, sd2);
write_array_into_tiff_image(out_name, out_image,
il, ie, ll, le);
free(elements);
} /* ends high_pixel */
/*******************************************
*
* low_pixel(..
*
* This function replaces the pixel at
* the center of a 3x3, 5x5, etc. area
* with the min for that area.
*
*******************************************/
low_pixel(in_name, out_name, the_image, out_image,
il, ie, ll, le, size)
char in_name[], out_name[];
int il, ie, ll, le, size;
short the_image[ROWS][COLS],
out_image[ROWS][COLS];
{
int a, b, count, i, j, k,
length, sd2, sd2p1, ss, width;
short *elements;
struct tiff_header_struct image_header;
sd2 = size/2;
sd2p1 = sd2 + 1;
/**********************************************
*
* Allocate the elements array large enough
* to hold size*size shorts.
*
**********************************************/
ss = size*size;
elements = (short *) malloc(ss * sizeof(short));
create_file_if_needed(in_name, out_name, out_image);
read_tiff_image(in_name, the_image, il, ie, ll, le);
/***************************
*
* Loop over image array
*
****************************/
printf("\n");
for(i=sd2; i<ROWS-sd2; i++){
if( (i%10) == 0) printf("%d ", i);
for(j=sd2; j<COLS-sd2; j++){
count = 0;
for(a=-sd2; a<sd2p1; a++){
for(b=-sd2; b<sd2p1; b++){
elements[count] = the_image[i+a][j+b];
count++;
}
}
sort_elements(elements, &ss);
out_image[i][j] = elements[0];
} /* ends loop over j */
} /* ends loop over i */
fix_edges(out_image, sd2);
write_array_into_tiff_image(out_name, out_image,
il, ie, ll, le);
free(elements);
} /* ends low_pixel */
/*******************************************
*
* median_filter(..
*
* This function performs a median filter
* on an image using a size (3x3, 5x5, etc.)
* specified in the call.
*
*******************************************/
median_filter(in_name, out_name, the_image, out_image,
il, ie, ll, le, size)
char in_name[], out_name[];
int il, ie, ll, le, size;
short the_image[ROWS][COLS],
out_image[ROWS][COLS];
{
int a, b, count, i, j, k,
length, sd2, sd2p1, ss, width;
short *elements;
struct tiff_header_struct image_header;
sd2 = size/2;
sd2p1 = sd2 + 1;
/**********************************************
*
* Allocate the elements array large enough
* to hold size*size shorts.
*